package com.buric.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;


/**
 * @author zhouwei
 * @date 2023/8/22 11:24
 */
@Slf4j
public class ComBeanUtils {

    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        //序列化的时候序列对象，bean的属性设置
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //反序列化的时候如果多了其它属性，不抛出异常
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //如果是空对象的时候，不抛异常
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        //取消时间的转换格式，默认是时间戳，同时需设置时间格式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    }


    public static ObjectMapper getMapper(){
        return mapper;
    }


    /**
     * 将 对象 转化成json字符串
     * @param o
     * @return
     */
    public static String toJson(Object o){
        if(o == null){
            return null;
        }
        try {
            return   mapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            log.error("object to json is error ",e);
        }
        return null;
    }


    /**
     * 将 字符串 转化成具体java对象
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T>T toObject(String json,Class<T> clazz){
        if(json == null) {
            return null;
        }
        try {
            return mapper.readValue(json,clazz);
        } catch (JsonProcessingException e) {
            log.error("json to object is error ",e);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 将 字符串 转化成 List
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(String json, Class<T> clazz){
        if(json == null) {
            return null;
        }
        JavaType javaType = getJavaCollection(List.class, clazz);
        try {
            return mapper.readValue(json,javaType);
        } catch (JsonProcessingException e) {
            log.error("json to object is error ",e);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将 对象 拷贝
     * @param o
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T>T copyObject(Object o, Class<T> clazz){
        if(o == null){
            return null;
        }
        return toObject(toJson(o),clazz);
    }


    /**
     * 集合拷贝
     * @param list
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> copyList(List<?> list, Class<T> clazz){
        if(list == null || list.isEmpty()){
            return null;
        }
        return toList(toJson(list),clazz);
    }



    private static JavaType getJavaCollection(Class<?> collectionType,
                                              Class<?> clazz){
        return mapper.getTypeFactory().constructCollectionLikeType(collectionType,clazz);
    }
}
